package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.SecurityDeviceInfo;
import com.example.prison.entity.SecurityList;
import com.example.prison.entity.vo.PatrolTrajectoryVO;
import com.example.prison.entity.vo.SecurityListVO;
import com.example.prison.mapper.SecurityDeviceInfoMapper;
import com.example.prison.mapper.SecurityListMapper;
import com.example.prison.service.SecurityListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author jiang
* @description 针对表【security_list】的数据库操作Service实现
* @createDate 2023-04-11 17:10:33
*/
@Service
public class SecurityListServiceImpl extends ServiceImpl<SecurityListMapper, SecurityList>
    implements SecurityListService {

    @Autowired
    private SecurityListMapper securityListMapper;

    @Autowired
    private SecurityDeviceInfoMapper securityDeviceInfoMapper;

    @Override
    public PageInfo<SecurityList> getSecurityList(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityListMapper.getSecurityList());
    }

    @Override
    public PageInfo<SecurityListVO> getInfoList(int pageNumber, int pageSize, String sn, String beginTime, String endTime) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityListMapper.getInfoList(sn,beginTime,endTime));
    }

    @Override
    public void addSecurityRecord(SecurityList securityList) {
        securityListMapper.addSecurityRecord(securityList);
    }

    @Override
    public void delete(Integer[] ids) {
        securityListMapper.deleteSecurityList(ids);
    }

    @Override
    public PageInfo<SecurityListVO> getListByTime(int pageNumber, int pageSize, String beginTime, String endTime) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityListMapper.getListByTime(beginTime, endTime));
    }

    @Override
    public PageInfo<SecurityListVO> getListBySN(int pageNumber, int pageSize, String sn) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityListMapper.getListBySN(sn));
    }

    @Override
    public SecurityList getById(Integer id) {
        return securityListMapper.getById(id);
    }

    @Override
    public PageInfo<PatrolTrajectoryVO> getPatrolTrajectory(int pageNumber, int pageSize) {
        String date  = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<PatrolTrajectoryVO> list = new ArrayList<>();
        List<SecurityDeviceInfo> deviceList = securityDeviceInfoMapper.get(null,null);
        if(ObjectUtils.isEmpty(deviceList.size())) {
            return new PageInfo<>(list);
        }
        for (SecurityDeviceInfo device : deviceList) {
            PatrolTrajectoryVO patrol = new PatrolTrajectoryVO();
            patrol.setSn(device.getDeviceNumber());
            patrol.setDeviceArea(device.getDeviceArea());
            List<SecurityListVO> record  = securityListMapper.getListBySN(device.getDeviceNumber());
            List<String> timeList = new ArrayList<>();
            for(SecurityListVO recordVO : record) {
                if(recordVO.getTime().substring(0, 10).equals(date)) {
                    timeList.add(recordVO.getTime());
                }
            }
            patrol.setTime(timeList);
            list.add(patrol);
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(list);
    }
}




